<?php

/**
 * Created by PhpStorm.
 * User: xuke
 * Date: 16/6/22
 * Time: 下午4:57
 */
namespace App\Http\Controllers\Api\Order;
use App\Http\Controllers\BaseController;
use App\Logic\OrderLogic;
use App\Logic\UserAddressLogic;
use DB;
use ApiResultFacade;
use phpDocumentor\Reflection\Types\This;

class OrderController extends BaseController
{
    public function createOrder()
    {
        $user = $this->_request->user();
        $orderLogic = OrderLogic::create();

        $orderLogic->setUser($user->user_id)->setOrderSn();//指定订单用户,订单号
        $goods = json_decode($this->_request->input('goods'), true); //接受所有参数信息
        $goods = collect($goods)->groupBy('type');//按类型分类,sku,bundle
        $address = $this->_request->input('address');//用户收货地址信息
        $addressLogic = UserAddressLogic::initByUserAddress($address);
        if ($addressLogic->getUserAddress()->user_id != $user->user_id)
        {

        }

        if ($goods->has('sku'))//如果类型有sku,保存sku
        {
            $orderLogic->setSkus($goods->get('sku')->toArray());
        }
        if ($goods->has('bundle'))
        {
            $orderLogic->setBundles($goods->get('bundle')->toArray());
        }

        //
        DB::beginTransaction();
        $orderLogic->setOrderNumAndPrice()->setUser($user->user_id)->setOrderAddress($addressLogic->getUserAddress())->saveOrder();
        DB::commit();

        return ApiResultFacade::setStatus(1)->setData($orderLogic->toApi())->ApiResult();

    }
}